package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.annotation.TingshuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserCollectVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/findUserSubscribePage/{pn}/{pz}")
    public Result findUserSubscribePage(@PathVariable(value = "pn") Long pn,
                                        @PathVariable(value = "pz") Long pz) {


        return Result.build(null, ResultCodeEnum.LOGIN_AUTH);
//        return Result.ok();
    }

    @GetMapping("/collect/{trackId}")
    @Operation(summary = "用户收藏声音")
    @Parameters({
            @Parameter(name = "trackId", description = "声音id", required = true)
    })
    @TingshuLogin
    public Result collect(@PathVariable Long trackId){
        Boolean flag = userInfoService.collect(trackId);

        return Result.ok(flag);
    }

    @GetMapping("/isCollect/{trackId}")
    @Operation(summary = "取消收藏")
    @Parameters({
            @Parameter(name = "trackId", description = "声音id", required = true)
    })
    public Result isCollect(@PathVariable Long trackId) {
        Boolean flag = userInfoService.isCollect(trackId);

        return Result.ok(flag);
    }

    @GetMapping("/isSubscribe/{albumId}")
    @Operation(summary = "是否订阅")
    @TingshuLogin
    @Parameters({
            @Parameter(name = "albumId", description = "专辑id", required = true)
    })
    public Result isSubscribe(@PathVariable(value = "albumId") Long albumId) {

        Boolean flag = userInfoService.isSubscribe(albumId);

        return Result.ok(flag);
    }


    @TingshuLogin
    @Operation(summary = "分页展示用户收藏列表")
    @GetMapping("/findUserCollectPage/{pn}/{pz}")
    @Parameters({
            @Parameter(name = "pn", description = "当前页", required = true),
            @Parameter(name = "pz", description = "每页记录数", required = true)
    })
    public Result findUserCollectPage(@PathVariable(value = "pn") Long pn, @PathVariable(value = "pz") Long pz) {
        IPage<UserCollectVo> page = new Page<>(pn, pz);
        page = userInfoService.findUserCollectPage(page, AuthContextHolder.getUserId());
        return Result.ok(page);
    }

}

